<?php

declare(strict_types=1);

namespace app\common\model;

use think\facade\Event;

class Paper extends Base
{

    protected $name = 'paper';

    public function getCateTextAttr($value, $data)
    {
        return Cate::where('id', $data['cate'])->value('title') ?? "";
    }

    public static function paperDelete($param, $logs): array
    {
        try {
            if (is_array($param['id'])) {
                self::destroy($param['id']);
                $model = (object)[];
                $logs['operation'] = 'deleteAll';
                $model->ids = $param['id'];
            } else {
                $model = self::where($logs['pk'], $param['id'])->find();
                self::destroy($param['id']);
                $logs['operation'] = 'delete';
            }
            if ($logs['writeLog']) {
                $logs['model'] = $model;
                Event::trigger('Logs', $logs);
            }
            PaperBank::destroy(['paper_id' => $param['id']], true);
            return ['code' => 1, 'msg' => '已删除'];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
    /**
     * 添加修改
     */
    public static function add_paper($param)
    {
        try {
            // print('<pre>');print_r($param);die;
            $info = parent::where('id', $param['paper_id'])->find();
            $cate = Cate::where('id', $info->cate)->find();
            if ($param['types'] == 1) {
                $param['radio'] = $param['ids'];
                if (isset($param['radio']) && count($param['radio']) != $cate['radio_num']) {
                    return ['code' => -1, 'msg' => '单选题数量数量为' . $cate['radio_num']];
                }
                PaperBank::destroy(['paper_id' => $param['paper_id'], 'types' => $param['types']], true);
                if (isset($param['radio'])) {
                    foreach ($param['radio'] as $key => $value) {
                        $radio_data[] = [
                            'paper_id' => $param['paper_id'],
                            'exam_id' => $value,
                            'cate' => $info->cate,
                            'types' => 1,
                            'create_time' => time(),
                            'update_time' => time(),
                        ];
                    }
                    if (!empty($radio_data)) {
                        PaperBank::insertAll($radio_data);
                    }
                }
            } 
            if($param['types'] == 2){
                $param['check'] = $param['ids'];
                if (isset($param['check']) && count($param['check']) != $cate['check_num']) {
                    return ['code' => -1, 'msg' => '多选题数量数量为' . $cate['check_num']];
                }
                if (isset($param['check'])) {
                    foreach ($param['check'] as $key => $value) {
                        $check_data[] = [
                            'paper_id' => $param['paper_id'],
                            'exam_id' => $value,
                            'cate' => $info->cate,
                            'types' => 2,
                            'create_time' => time(),
                            'update_time' => time(),
                        ];
                    }
                    if (!empty($check_data)) {
                        PaperBank::insertAll($check_data);
                    }
                }
            }
            return ['code' => 1, 'msg' => '添加成功！'];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}
